package org.finmanager.budget;

import com.alee.extended.layout.TableLayout;
import com.alee.laf.panel.WebPanel;
import org.finmanager.core.Category;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static org.finmanager.core.Constants.BUDGET_INPUT_UI_NAME;
import static org.finmanager.core.Constants.BUDGET_INPUT_UI_WIDTH;
import static org.finmanager.core.Constants.BUDGET_INPUT_UI_HEIGHT;
import static org.finmanager.core.Constants.BUDGET_INPUT_UI_RESIZABLE;

/**
 * Created by Kelvin on 4/4/2014.
 */

//users are able to input budget in the planning function by typing the "+" button
//information needed: 1.amount
//                    2.year
//                    3.month
//                    4.category(will using a override combo box)
public class BudgetInputUI {
	private JPanel mainPanel = new JPanel();

	private JFrame frame;

	private BudgetManagerController budgetManagerController;

	private JTextField amountTextField;
	private JTextField yearTextField;
	private JTextField monthTextField;
	private JComboBox categoryComboBox;

	private JButton okayButton;

	public BudgetInputUI(BudgetManagerController budgetManagerController) {
		this.budgetManagerController = budgetManagerController;

		createAndShowGUI();

		okayButton.setActionCommand(BudgetManagerController.ADD_BUDGET);
		okayButton.addActionListener(budgetManagerController.getActionListener());
	}

	private void createAndShowGUI() {
		WebPanel inputPanel = new WebPanel();

		TableLayout tableLayout = new TableLayout(new double[][]{{TableLayout.PREFERRED, TableLayout.FILL},
				{TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}}, 10, 3);
		inputPanel.setLayout(tableLayout);
		inputPanel.setMargin(15, 30, 15, 30);

		amountTextField = new JTextField("", 10);
		yearTextField = new JTextField("", 10);
		monthTextField = new JTextField("", 10);
		categoryComboBox = createComboBox(budgetManagerController.getCategoryModel().getBudgetCategoryComboBoxModel());

		inputPanel.add(new JLabel("Amount", JLabel.TRAILING), "0, 0");
		inputPanel.add(amountTextField, "1, 0");
		inputPanel.add(new JLabel("Year", JLabel.TRAILING), "0, 1");
		inputPanel.add(yearTextField, "1, 1");
		inputPanel.add(new JLabel("Month", JLabel.TRAILING), "0, 2");
		inputPanel.add(monthTextField, "1, 2");
		inputPanel.add(new JLabel("Category", JLabel.TRAILING), "0, 3");
		inputPanel.add(categoryComboBox, "1, 3");


		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

		okayButton = new JButton("OK");
		JButton cancelButton = new JButton("Cancel");

		okayButton.addActionListener(new CloseWindowListener());
		cancelButton.addActionListener(new CloseWindowListener());

		buttonPanel.add(okayButton);
		buttonPanel.add(cancelButton);

		getMainComponent().setLayout(new BorderLayout(0, 0));
		getMainComponent().add(inputPanel, BorderLayout.CENTER);
		getMainComponent().add(buttonPanel, BorderLayout.SOUTH);


		frame = new JFrame(BUDGET_INPUT_UI_NAME);
		frame.setSize(new Dimension(BUDGET_INPUT_UI_WIDTH, BUDGET_INPUT_UI_HEIGHT));
		frame.setLocationRelativeTo(budgetManagerController.getUIMainComponent());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.setResizable(BUDGET_INPUT_UI_RESIZABLE);

		frame.add(getMainComponent());

		frame.setVisible(true);
	}

	private JComboBox createComboBox(ComboBoxModel comboBoxModel) {
		JComboBox comboBox = new JComboBox(comboBoxModel);
		comboBox.setSelectedIndex(0);
		return comboBox;
	}

	public Budget getBudgetFromInput() {
		Category category = (Category) categoryComboBox.getSelectedItem();
		Double amount = Double.parseDouble(amountTextField.getText());
		int year = Integer.parseInt(yearTextField.getText());
		int month = Integer.parseInt(monthTextField.getText()) - 1;

		return new Budget(category, amount, amount, year, month);
	}

	public JComponent getMainComponent() {
		return mainPanel;
	}

	private class CloseWindowListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			frame.dispose();
		}
	}
}
